
$(function(){
    advance_search.init();
});

var advance_search = {
    MODULE_ID : "",
    Table_Obj : null, // 表格对象
    pageIndex : 1,
    pageSize : 10,
    jump_page : 0,
    USER_:null,
	init : function() {
		advance_search.USER_= getSessoin();
        advance_search.MODULE_ID = GetRequest().pid;
        $(document).keydown(function (event) {
            if (event.keyCode === 13) {
                advance_search.getData();
            }
        })
    },
    /**
     * 添加条件
     * @param _this
     */
    addCondition : function(_this) {
        // var n = $(".search_conditions .condition");
		// 	if(n.length>=5){
		// 	}
        var html ="";
        html += '<div class="condition">';
        html += '<select>';
        html += '<option value="AND">与</option>';
        html += '<option value="OR">或</option>';
        html += '<option value="NOT">非</option>';
        html += '</select>';
        html += '<select  class="filed">';
        html += '<option value="ent_name">名称</option>';
        html += '<option value="ent_address">地址</option>';
        html += '<option value="ent_one_person">联系人</option>';
        html += '<option value="ent_industry_one">行业</option>';
        html += '<option value="ent_product">产品</option>';
        html += '<option value="ent_mode">模式</option>';
        html += '<option value="ent_needs_urgent">需求</option>';
        html += '</select>';
        html += '<input type="text" placeholder="请输入检索词"/>';
        html += '<select>';
        html += '<option value="1">模糊</option>';
        html += '<option value="0">精确</option>';
        html += '</select>';
        html += '<input type="button" value="+" class="btn btn-info" onclick="advance_search.addCondition(this)"/>';
        html += '<input type="button" value="-" class="btn btn-danger" onclick="advance_search.removeCondition(this)"/>';
        html += '</div>';
        $(_this).parent("div").after(html)
    },
    /**
     * 移除条件
     *
     * @param _this
     */
    removeCondition : function(_this) {
        $(_this).parent("div").remove();
    },
    /**
     * 清空所有条件
     */
    removeAllConditions : function() {
        advance_search.pageIndex = 1;
        advance_search.jump_page = 0;
        // advance_search.search_type="talent";
        // advance_search.agency_type="school";
        var contents = [];
        var n = $(".search_conditions .condition");
        $(n).each(function(i,row){
            if(i>1){
                $(row).remove();
            }
        });
        $(".search_conditions .isBool").val("AND");
        $(".search_conditions .LikeOrEq").val("0");
        $(".search_conditions input[type=text]").val("");
    },
    /**
     * 获取检索条件
     */
    getParam : function() {
        var obj = {};
        var contents = [];
        var n = $(".search_conditions .condition");
        $(n).each(function(i, row) {
            var content = [];
            var ch = row.children;
            if($(ch[0]).val().trim().toString() === "NOT") {
                content.push("AND");
                content.push("-" + $(ch[1]).val().trim());
            } else {
                content.push($(ch[0]).val().trim());
                content.push($(ch[1]).val().trim());
            }
            content.push($(ch[2]).val().trim());
            content.push($(ch[3]).val().trim());
            content.join(',');
            contents.push(content);
        });
        obj.higthText=contents.join('@#@')+"@#@AND,ent_stats,0,0";
        obj.pageIndex = 1;
        pageSize = $("#page_size").val();
        obj.pageSize = pageSize;
        obj.orderBy = "create_time";
        obj.orderDesc = 1;
        obj.table = "data_enterprise";
        obj.text = "";
        return obj;
    },
    /**
     * 获取数据
     */
    getData : function() {
        var obj = advance_search.getParam();
        $.ajax({
            url : "/crm/retrieval/comSearch",
            dataType : "json",
            data : obj,
            type : "post",
            success : function(data) {
                advance_search.initTable(data.rows);
                $("#total_records").text(data.total);
                $("#this_page").text(1);
                $("#total_page").text(data.totalPages);
                var url = "/crm/retrieval/comSearch";
                setCommon(obj, Table_Obj, url);
            },
            error : function() {
            }
        })
    },
    /**
     * 初始化表格
     */
    initTable : function(datas) {
        Table_Obj = $("#TableContainer").easyTable({
            data : datas, // 初始数据，动态添加可以通过setData
            hideCheckbox : false, // 否显示复选框，获取复选框选择的数据用方法getCheckedItem
            clickChangeColor:false,//单击之后切换颜色，
			doubleClickChangeColor:true,//双击之后切换颜色，
            // rowClick : function(data) { // 行点击回调，参数为改行数据
            // alert(JSON.stringify(data))
            // },
            rowDoubleClick : function(data) { // 行双击回调，参数为改行数据
                advance_search.lookDetail(data.uuid);
            },
            columns : [ {
                title : "名称",
                field : "ent_name"
            }, {
                title : "行业大类",
                field : "ent_industry_one",
                render : function (data) {
                    if(!isBlank(data.ent_industry_one)) {
                        try {
                            return JSON.parse(data.ent_industry_one).indName
                        } catch(e) {
                            console.log("转换行业大类失败");
                        }
                    }
                    return "";

                }
            }, {
                title : "行业子类",
                field : "ent_industry_two"
            }, {
                title : "上游企业",
                field : "ent_upstream"
            }, {
                title : "下游企业",
                field : "ent_downstream"
            }, {
                title : "股权",
                field : "ent_stock_right"
            }, {
                title : "创建时间",
                field : "create_time"
            }, {
                title : "上次编辑",
                field : "last_edit_time"
            }, {
                title : "操作",
                render : function(data) {
                    return advance_search.loadOperator(data);
                }
            } ]
        });
    },
    /**
     * 加载方法
     *
     * @param data
     * @returns {String}
     */
    loadOperator : function(data) {
        var html = "";
        html += '<a onclick="advance_search.lookDetail(\'' + data.uuid + '\',this)">查看</a>';
        var str = data.edit_power_user_uuids.indexOf(advance_search.USER_.uuid);
		if(str!= -1){
			html += '<a class="ml10" onclick="advance_search.edit(' + data.id + ')">编辑</a>';
		}
        return html;
    },
    /**
     * 检索
     */
    searchData : function() {
        var pageSize = $("#page_size").val();
        var pageIndex = 1;
        var obj = {};
        obj.pageSize = pageSize;
        obj.pageIndex = pageIndex;
        reset_serach(advance_search.getParam(), obj);
    },
    /**
     * 查看详情
     */
    lookDetail : function(uuid,_this) {
    	$(_this).parent().parent("tr").css("color", "red");
        parent.createPage("企业详情", "../html/enterprise/enterprise_detail.html?uuid=" + uuid, true, "enterprise_detail");
    },
    /**
     * 编辑项目
     */
    edit : function(id) {
        parent.createPage("编辑企业", "../html/enterprise/enterprise_edit.html?id=" + id, true, "enterprise_edit");
    },
	/**
	 * 导出表格
	 */
	exportExcel:function(){
		if(Table_Obj==null){
			layer.alert("请先检索数据！");
			return;
		}
		var rows = Table_Obj.getCheckedItem();
		var column = new Array();
		column.push(Table_Obj.getColumns());
		var dataList={};
		dataList.column=column;
		dataList.rows=rows;
		dataList.type="enter";
		if(rows.length == 0){
			layer.alert("未选择导出的数据！");
		}else{
	        var tempForm = document.createElement("form");
	        document.body.appendChild(tempForm);
	        tempForm.method="post";
	        tempForm.action="/crm/exportExcel";
	        var hideInput = document.createElement("input");
	        hideInput.type="hidden";
	        hideInput.name= "datas";
	        hideInput.value= JSON.stringify(dataList);
	        tempForm.appendChild(hideInput);
	        tempForm.submit();
	    }
	},
	/**
	 * 导出查询出的所有数据
	 */
	exportAllToExcel:function(){
		if(Table_Obj==null){
			layer.alert("请先检索数据！");
			return;
		}
		var pageSize = $("#total_records").text();
		var pageIndex = 1;
		var obj = advance_search.getParam();
		obj.pageSize = pageSize;
		obj.pageIndex = pageIndex;
		var column = new Array();
		column.push(Table_Obj.getColumns());
		obj.column=JSON.stringify(column);
		var tempForm = document.createElement("form");
        document.body.appendChild(tempForm);
        tempForm.method="post";
        tempForm.action="/crm/enterprise/exportExcel";
        var hideInput = document.createElement("input");
        hideInput.type="hidden";
        hideInput.name= "datas";
        hideInput.value= JSON.stringify(obj);
        tempForm.appendChild(hideInput);
        tempForm.submit();
	}
};